$(document).ready(function() {
	$("#demo").validate({
		rules:{
			"students.stuNum": {
				required: true,
	    		maxlength: 10
			},
			"students.stuName":{
				required: true,
	    		maxlength: 10
			},
			"students.departmentId":"required",
			"students.majorId":"required",
			"students.classId":"required"
		}
	});
});

//返回department的json数据，带被选择数据
function queryDepartmentList(departmentId){
	$.post(
		"../AjaxResultData/listAllDepartment.action",
		function(data){
			$("#departmentId").empty();
			$("#departmentId").append("<option value=''>--未选择--</option>");
			var departmentData = eval("("+data.departmentData+")");  
			for(var i = 0;i<departmentData.length;i++){
				if(departmentId==departmentData[i].departmentId){
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"' selected='selected'>"+departmentData[i].departmentName+"</option>");
				}else{
					$("#departmentId").append("<option value='"+departmentData[i].departmentId+"'>"+departmentData[i].departmentName+"</option>");
				}
			}
		},
			"json"
	);
}

//根据DepartmentId返回major的json数据
function queryMajorByDepartmentId(departmentId,majorId){
	$.post(
		"../AjaxResultData/listAllMajorByDepartmentId.action",
		{"major.departmentId":departmentId},
		function(data){
			$("#majorId").empty();
			$("#majorId").append("<option value=''>--未选择--</option>");
			var majorData = eval("("+data.majorDataByDepartmentId+")");
			for(var i = 0; i<majorData.length; i++){
				if(majorId==majorData[i].majorId){
					$("#majorId").append("<option value='"+majorData[i].majorId+"' selected='selected'>"+majorData[i].majorName+"</option>");	
				}else{
					$("#majorId").append("<option value='"+majorData[i].majorId+"'>"+majorData[i].majorName+"</option>");
				}
			}
		}
	);
}

//根据MajorId返回classStu的json数据
function queryClassByMajorId(majorId,classId){
	$.post(
		"../AjaxResultData/listAllClassByMajorId.action",
		{"classStu.majorId":majorId},
		function(data){
			var classData = eval("("+data.classData+")");
			$('#classId').empty();
			$('#classId').append("<option value=\"\">--请选择--</option>");
			for(var i = 0; i<classData.length; i++){
				if(classId==classData[i].classId){
					$('#classId').append("<option value='"+classData[i].classId+"' selected='selected'>"+classData[i].className+"</option>");
				}else{
					$('#classId').append("<option value='"+classData[i].classId+"'>"+classData[i].className+"</option>");
				}
			}
		},
		"json"
	);
}

//验证学号是否已被使用
function checkStuNumIsNotUsed(){

	var stuNum = document.getElementById("stuNum").value;

	if(stuNum!=""){
		$.post(
				"../AjaxResultData/checkStuNumIsNotUsed.action",
				{"students.stuNum":stuNum},
				function(data){
					var isNotUsedData = eval("("+data.isNotUsedData+")");
					if(isNotUsedData[0].isNotUsed=="true"){
						var stuNumTemp = $("#stuNum").attr("value");
						$("#ajaxShowResult").html("学号"+stuNumTemp+"已存在，不能重复添加！");
						$("#stuNum").attr("value","");
					}else if(isNotUsedData[0].isNotUsed=="false"){
						$("#ajaxShowResult").html("学号可用");
					}
				},
				"json"
		);
	}else{
		document.getElementById("ajaxShowResult").innerText = "";
	}
}

function createDepartmentChange(majorId){
	$("#departmentId").change(function(){
		var departmentId = document.getElementById("departmentId").value;
		$("#majorId").empty();
		$("#majorId").append("<option value=\"\">--请选择--</option>");
		$("#classId").empty();
		$("#classId").append("<option value=\"\">--请选择--</option>");
		queryMajorByDepartmentId(departmentId,majorId);
	});
}

function createMajorChange(classId){
	$("#majorId").change(function(){
		var majorId = document.getElementById("majorId").value;
		$("#classId").empty();
		$("#classId").append("<option value=\"\">--请选择--</option>");
		queryClassByMajorId(majorId,classId);
	});
}

function initForm(departmentId,majorId,classId){
	queryDepartmentList(departmentId);
	queryMajorByDepartmentId(departmentId,majorId);
	queryClassByMajorId(majorId,classId);
}